package activitytest.example.com.application;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import activitytest.example.com.application.utils.SpUtils;

/**
 * Created by zhanyihong on 2017/12/5.
 */

public class HomeActivity extends Activity{
    private GridView gv_home;
    private  String[] titleStr;
    private int[] drawableIds;
       @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initUI();
        initData();
    }
    
    private void initData() {
        titleStr =new String[]{
          "手机防盗","通信卫士","软件管理","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"
        };
         drawableIds = new int[]{
                R.drawable.safe, R.drawable.safe, R.drawable.safe, R.drawable.safe, R.drawable.safe,
                R.drawable.safe, R.drawable.safe, R.drawable.safe, R.drawable.safe
        };
        gv_home.setAdapter(new MyAdapter());
        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0:
                        showDialog();

                        break;
                    case 8:
                       Intent intent= new Intent(getApplicationContext(),SettingActivity.class);
                       startActivity(intent);
                        break;
                }
            }

            private void showDialog() {
                showSetPsdDialog();
//                 showConfirmPsdDialog
            }
        });
    }

    private void showSetPsdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this,R.layout.dialog_set_psd,null);
        dialog.setView(view);
        dialog.show();

        Button bt_submit =(Button) view.findViewById(R.id.bt_submit);
        Button bt_cancel=(Button) view.findViewById(R.id.bt_cancel);
//
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(getApplicationContext(), SetupOverActivity.class);
                startActivity(intent1);
                dialog.dismiss();
            }
        });
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    private void showConfirmPsdDialog() {
    }

    private void initUI() {
        gv_home = (GridView) findViewById(R.id.gv_home);
    }

   class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return titleStr.length;
        }

        @Override
        public Object getItem(int i) {
            return titleStr[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
           View veew = View.inflate(getApplicationContext(),R.layout.gridview_item ,null);
           TextView tv_title =(TextView) veew.findViewById(R.id.tv_title);
           ImageView iv_icon =(ImageView) veew.findViewById(R.id.iv_icon);

           tv_title.setText(titleStr[i]);
           iv_icon.setBackgroundResource(drawableIds[i]);
            return veew;
        }
    }
}
